package com.haoshuang.zhouzhoubang.ui.activity

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.ActivityAddressManagementBinding
import com.haoshuang.zhouzhoubang.model.bean.AddressBean
import com.haoshuang.zhouzhoubang.ui.adapter.AddresManagementAdapter
import com.haoshuang.zhouzhoubang.ui.page.shop.address.add.AddAddressActivity
import com.haoshuang.zhouzhoubang.ui.vm.AddressManagementViewModel
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.core.setOnItemChildClick
import com.mufeng.libs.core.setupData
import com.mufeng.libs.core.setupEmptyData
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger

/**
 *  地址管理界面
 */
class AddressManagementActivity :
    BaseVMActivity<AddressManagementViewModel, ActivityAddressManagementBinding>() {
    val mAdapter = AddresManagementAdapter(mutableListOf())
    override fun initView(savedInstanceState: Bundle?) {
        val layoutManager = LinearLayoutManager(this)    // 创建布局管理者
        var type = intent.getIntExtra("type", 1)
        binding.apply {
            appBar.ivBack.clickWithTrigger { finish() }
            appBar.tvTitle.text = "地址管理"

            recyclerView.layoutManager = layoutManager
            recyclerView.adapter = mAdapter
            llAddAddress.setOnClickListener { startPage<AddAddressActivity>("ids" to 0) }

            mAdapter.setOnItemChildClick(id = R.id.textView38) { view, position, item ->
                startPage<AddAddressActivity>("ids" to list[position].id, "bean" to list[position])
            }

            mAdapter.setOnItemChildClick(id = R.id.textView40) { view, position, item ->
                vm.getData2("addressId" to list[position].id)
            }

            mAdapter.setOnItemChildClick(id = R.id.ivRadioIcon) { view, position, item ->
                vm.getData3("addressId" to list[position].id)
            }
        }
    }

    override fun onRestart() {
        super.onRestart()
        p = 1
        getNet()
    }

    var list = arrayListOf<AddressBean>()
    override fun initData() {

        binding.apply {
            refreshLayout.setOnRefreshListener {
                p = 1
                getNet()
            }
            refreshLayout.setOnLoadMoreListener {

                p++
                getNet()
            }
            p = 1
            getNet()
        }
    }

    private fun getNet() {
        vm.getData("page" to p, "limit" to 10)
    }

    override fun startObserve() {
        vm.getData2.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                if (code != "402") {
                    toast(msg)
                }
            }
            onSuccess {
                dismissLoading()
                toast("删除成功")
                p = 1
                getNet()
            }
        }
        vm.getData3.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                if (code != "402") {
                    toast(msg)
                }
            }
            onSuccess {
                dismissLoading()
                toast("设置成功")
                p = 1
                getNet()
            }
        }
        vm.getData.observeState(this) {
            onLoading { }
            onError { code, msg ->
                dismissLoading()
                if (code != "402") {
                    toast(msg)
                }
                binding.refreshLayout.setupEmptyData(list, mAdapter, p)
            }
            onSuccess {
                dismissLoading()
                binding.refreshLayout.setupData(it,list, mAdapter, p)
            }
        }
    }

}